home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / libgutil / dit.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  2KB  |  97 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /* 
  18.  *    dit - 
  19.  *        Dither pixels into 1 bit deep bits.
  20.  *
  21.  *
  22.  */
  23. #define MATSIZE88
  24.  
  25. #define DXSIZE    8
  26. #define DYSIZE    8
  27.  
  28. static int ditscaled; 
  29.  
  30. static short dithmat[DYSIZE][DXSIZE] = {    /* halftone dots */
  31.     3,    17,    55,     63,    61,     47,    9,    1,
  32.     15,     29,    39,    51,    49,    35,    25,    13,
  33.     40,    32,    26,    20,    22,    30,    36,    42,
  34.     56,    44,    10,    4,    6,    18,    52,    58,
  35.     60,    46,    8,    0,    2,    16,    54,    62,
  36.     48,    34,    24,    12,    14,    28,    38,    50,
  37.     23,    31,    37,    43,    41,    33,    27,    21,
  38.     7,    19,    53,    59,    57,    45,    11,    5,
  39. };
  40.  
  41. #define TOTAL        (DXSIZE*DYSIZE)
  42.  
  43. dittobits(sbuf,bits,y,n)
  44. short *sbuf;
  45. char *bits;
  46. int y, n;
  47. {
  48.     short val;
  49.     int rshades, rmaxbits;
  50.     short *rdith, *rptr;
  51.     short mask;
  52.     int dx, dy;
  53.  
  54.     if(!ditscaled) {
  55.     for(dy=0; dy<DYSIZE; dy++)
  56.         for(dx=0; dx<DXSIZE; dx++)
  57.         dithmat[dy][dx] = (254*dithmat[dy][dx])/63;
  58.     ditscaled = 1;
  59.     }
  60.     rdith = &dithmat[y%DYSIZE][0];
  61.     mask = 0x80;
  62.     rptr = rdith;
  63.     val = 0;
  64.     while(n) {
  65.     if(n>=8) {
  66.         if(sbuf[0] <= rptr[0])
  67.         val |= 0x80;
  68.         if(sbuf[1] <= rptr[1])
  69.         val |= 0x40;
  70.         if(sbuf[2] <= rptr[2])
  71.         val |= 0x20;
  72.         if(sbuf[3] <= rptr[3])
  73.         val |= 0x10;
  74.         if(sbuf[4] <= rptr[4])
  75.         val |= 0x08;
  76.         if(sbuf[5] <= rptr[5])
  77.         val |= 0x04;
  78.         if(sbuf[6] <= rptr[6])
  79.         val |= 0x02;
  80.         if(sbuf[7] <= rptr[7])
  81.         val |= 0x01;
  82.         sbuf+=8;
  83.         rptr = rdith;
  84.         *bits++ = val;
  85.         val = 0;
  86.         n -= 8;
  87.     } else {
  88.         if(*sbuf++ <= *rptr++)
  89.         val |= mask;
  90.         mask >>= 1;
  91.         n--;
  92.     }
  93.     }
  94.     if(mask!=0x80)
  95.     *bits++ = val;
  96. }
  97.